<?php

namespace app\admin\model;

use think\Model;


class Pmsorderinfo extends Model
{
    // 表名
    protected $name = 'pms_order_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $resultSetType = 'collection';

    // 追加属性
    protected $append = [
        'check_in_time_text',
        'check_out_time_text'
    ];


    public function getCheckInTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_in_time']) ? $data['check_in_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCheckOutTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_out_time']) ? $data['check_out_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCheckInTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCheckOutTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function get_order_status_name($order_status)
    {
        $re = '作废';
        switch ($order_status) {
            case -1:
                $re = '已受理';
                break;
            case 0:
                $re = '已住院';
                break;
            case 1:
                $re = '已出院';
                break;
        }

        return $re;
    }

    public function create_order_no($code)
    {
        $re  = '';
        $ymd = date('ymd');
        $m   = $this->field('order_no')->order('id desc')->find();

        if ($m) {
            $order_no = $m['order_no'];

            $order_no = substr($order_no, strlen($code), strlen($order_no) - strlen($code));
            $numstr   = '001';
            if (substr($order_no, 0, strlen($ymd)) == $ymd) {
                $num    = substr($order_no, strlen($ymd), strlen($order_no)) + 0 + 1;
                $numstr = '00' . $num;
                $numstr = substr($numstr, strlen($numstr) - 3, 3);
            }
            $re = $ymd . $numstr;
        } else {
            $re = $ymd . '001';
        }

        return $code . $re;
    }
}
